package core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;

/**
 *
 * @author Marcin
 */

enum Status{
    Admin(1),
    Medyk(2),
    Pacjent(3),
    Prezes(4),
    Praktykant(5);
    int status;
    Status(int status)
    {
        this.status = status;
    }   
}

public class Session {
    private JFrame parent;
    private boolean isLogged;
    private Status status;
    public Session()
    {
        parent = null;
        isLogged = false;
    }
    public Session(JFrame parent)
    {
        this.parent = parent;
        isLogged = false;
    }
    public boolean logIn(String username,String password)
    {
        try {
            PreparedStatement getpassword = SqlConnection.getInstance().getSqlConnection().prepareStatement("SELECT id_uprawnienia,password,is_active FROM user WHERE login=?");
            getpassword.setString(1, username);
            ResultSet rs = getpassword.executeQuery();
            if(rs.next() &&(rs.getString(2) != null) && rs.getString(2).equals(password) && rs.getBoolean(3)) {
                isLogged = true;
                status =  Status.values()[rs.getInt(1)-1];
                return true;
            }
            else {
                ErrorLogger.getInstance().log("Niepoprawny login lub hasło", true, parent);
                return false;
            }
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true, parent);
        }
        return false;
    }
    public boolean isLogged()
    {
        return isLogged;
    }
    public Status getStatus()
    {
        return status;
    }
    
}
